#include "threadPool.h"

int recvn(int sockfd, void *buf, int length){
    int total = 0; //已经接收的长度
    char *p = (char *)buf; // char的单位为1
    while(total < length){
        ssize_t sret = recv(sockfd,p+total,length-total,MSG_NOSIGNAL);
        total += sret;
    }
    return 0;
}

int sendn(int sockfd, void *buf, int length) {
    int total = 0; //已经发送的长度
    char *p = (char *)buf; // char的单位为1
    while(total < length) {
        ssize_t sret = send(sockfd, p + total, length - total, MSG_NOSIGNAL);
        total += sret;
    }
    return 0;
}

int handle_pwd(int netfd){
    char *ret = getcwd(NULL, 0);
    printf("ret = %s\n", ret);
    ssize_t sret = send(netfd, ret, strlen(ret), MSG_NOSIGNAL);
    printf("sret = %ld\n", sret);

    return 0;
}
int handle_cd(int netfd, char *arg){
    //char* args=arg;
    if(strlen(arg)==1){
        //回到家目录
    }else{
        //char* args_new= strtok(args," ");
        //  /wangpanxiangmu/netdisk/server/cdtest1
        //printf("%s\n",args_new);

        arg[0] = '/';
        char *ret = getcwd(NULL, 0);
        sprintf(ret, "%s%s", ret, arg);
        printf("ret after: %s\n", ret);
        printf("strlen(ret) = %ld\n", strlen(ret));
        //args_new= strncpy(args_new,ret,strlen(ret)-1);
        // printf("%s\n",args_new);
        // printf("ret = %s\n", ret);
        //  args_new= strcat( strcat(ret,"/"),args_new);
        // printf("%s\n",args_new);
        // printf("%ld\n",strlen(args_new));
        // 
        // int p=chdir("/home/lgd/wangpanxiangmu/netdisk/server/cdtest1");
        //printf("%d\n",p);

        int result = chdir(ret);
        printf("chdir ret = %d\n", result);
        if(result != 0){
            printf("chdir ret = %d\n", result);
            ssize_t sret = send(netfd, "No such directory", 17, MSG_NOSIGNAL);
        }else{
            char *ret = getcwd(NULL, 0);
            printf("ret = %s\n", ret);
            //ssize_t sret = send(netfd, ret, strlen(ret),MSG_NOSIGNAL);
            ssize_t sret = send(netfd, "cd successed!", 13, MSG_NOSIGNAL);

        }

    }
    return 0;
}

int handle_ls(int netfd, char *arg){

    char* path = (char*)malloc(PATH_MAX * sizeof(char));
    if (path == NULL) {
        printf("内存分配失败\n");
        return NULL;
    }

    if (getcwd(path, PATH_MAX) == NULL) {
        printf("无法获取当前目录\n");
        free(path);
        return NULL;
    }

    DIR* directory = opendir(path);
    if (directory == NULL) {
        printf("无法打开目录 %s\n", path);
        free(path);
        return NULL;
    }

    struct dirent* entry;
    char* listing = NULL;
    size_t listingSize = 0;

    while ((entry = readdir(directory)) != NULL) {
        if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
            size_t entryNameLength = strlen(entry->d_name);
            listing = realloc(listing, listingSize + entryNameLength + 1);
            strcat(listing + listingSize, entry->d_name);
            listingSize += entryNameLength;
            listing[listingSize++] = ' ';
        }
    }

    closedir(directory);

    if (listingSize > 0) {
        // 将最后一个空格替换为字符串结尾符 \0
        listing[listingSize - 1] = '\0';
    }

    free(path);
    
    printf("ls_info:\n");
    if (listing != NULL) {
        printf("当前目录下的文件和目录名：%s\n", listing);
        //sendn(netfd, listing, strlen(listing));

        ssize_t sret = send(netfd, listing, strlen(listing), MSG_NOSIGNAL);
        free(listing);
    }

    return 0;
}

int handle_mkdir(int netfd, char *arg){
     // 创建目录
    char* arg_new= strtok(arg," ");
    printf("arg_new:%s",arg_new);
    if (mkdir(arg_new, 0777) == -1) {
        printf("无法创建目录 %s\n", arg_new);
        ssize_t sret = send(netfd, "mkdir failure", 13 , MSG_NOSIGNAL);
        return NULL;
    }

    // 分配内存并复制目录地址
    size_t addressLength = strlen(arg_new);
    char* newarg = (char*)malloc((addressLength + 1) * sizeof(char));
    if (newarg == NULL) {
        printf("内存分配失败\n");
        return NULL;
    }
    ssize_t sret = send(netfd, "mkdir success", 13 , MSG_NOSIGNAL);
    return 0;
}
int handle_remove(int netfd, char *arg){
    
    char* arg_new= strtok(arg," ");
    printf("arg_new: %s\n",arg_new);
    if (rmdir(arg_new)==0){
        printf("文件删除成功\n");
        ssize_t sret = send(netfd, "rmdir success", 13 , MSG_NOSIGNAL);
    }else{
        printf("文件删除失败\n");   
        ssize_t sret = send(netfd, "rmdir failure", 13 , MSG_NOSIGNAL);
    }

    return 0;
}
int handle_puts(int netfd, char *arg){

    return 0;
}
int handle_gets(int netfd, char *arg){

    return 0;
}

int handle_command(int netfd, char *cmd, char *arg){
    if(strncmp(cmd, "pwd", 3) == 0){
        handle_pwd(netfd);
    }
    else if(strncmp(cmd, "cd", 2) == 0){
        handle_cd(netfd, arg);
    }
    else if(strncmp(cmd, "ls", 2) == 0){
        handle_ls(netfd, arg);
    }
    else if(strncmp(cmd, "mkdir", 5) == 0){
        handle_mkdir(netfd, arg);
    }
    else if(strncmp(cmd, "remove", 5) == 0){
        handle_remove(netfd, arg);
    }
    else if(strncmp(cmd, "puts", 4) == 0){
        handle_puts(netfd, arg);
    }
    else if(strncmp(cmd, "gets", 4) == 0){
        handle_gets(netfd, arg);
    }

    return 0;
}
